package br.ufrgs.meetapp.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Helper class to create and manage the database
 * @author Fernando
 *
 */
public class MFTDatabase extends SQLiteOpenHelper {

	// database and unique table values
	
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "MFTDatabase";
	
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_VALUE = "value";
    public static final String EVENT_MESSAGE_TABLE_NAME = "EVENT_MESSAGE";
    
    
    public static final String LAST_VALID_LOCATION_ID = "_id";
    public static final String LAST_VALID_LOCATION_ACCURACY = "accuracy";
    public static final String LAST_VALID_LOCATION_ALTITUDE = "altitude";
    public static final String LAST_VALID_LOCATION_BEARING = "bearing";
    public static final String LAST_VALID_LOCATION_LATITUDE = "latitude";
    public static final String LAST_VALID_LOCATION_LONGITUDE = "longitude";
    public static final String LAST_VALID_LOCATION_PROVIDER = "provider";
    public static final String LAST_VALID_LOCATION_SPEED = "speed";
    public static final String LAST_VALID_LOCATION_TIME = "time";
    public static final String LAST_VALID_LOCATION_HAS_ACCURACY = "has_accuracy";
    public static final String LAST_VALID_LOCATION_HAS_ALTITUDE = "has_altitude";
    public static final String LAST_VALID_LOCATION_HAS_BEARING = "has_bearing";
    public static final String LAST_VALID_LOCATION_HAS_SPEED = "has_speed";
    public static final String LAST_VALID_LOCATION_TABLE_NAME = "LAST_VALID_LOCATION";
    
    
    private static final String EVENT_MESSAGE_TABLE_CREATE =
                "CREATE TABLE " + EVENT_MESSAGE_TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_VALUE + " BLOB NOT NULL);";
    
    private static final String LAST_VALID_LOCATION_TABLE_CREATE =
            "CREATE TABLE " + LAST_VALID_LOCATION_TABLE_NAME + " (" +
            LAST_VALID_LOCATION_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            LAST_VALID_LOCATION_ACCURACY + " STRING, " +
            LAST_VALID_LOCATION_ALTITUDE + " STRING, " +
            LAST_VALID_LOCATION_BEARING + " STRING, " +
            LAST_VALID_LOCATION_LATITUDE + " STRING, " +
            LAST_VALID_LOCATION_LONGITUDE + " STRING, " +
            LAST_VALID_LOCATION_PROVIDER + " STRING, " +
            LAST_VALID_LOCATION_SPEED + " STRING, " +
            LAST_VALID_LOCATION_TIME + " STRING, " +
            LAST_VALID_LOCATION_HAS_ACCURACY + " INTEGER, " +
            LAST_VALID_LOCATION_HAS_ALTITUDE + " INTEGER, " +
            LAST_VALID_LOCATION_HAS_BEARING + " INTEGER, " +
            LAST_VALID_LOCATION_HAS_SPEED + " INTEGER);";

    /**
     * Creates an object of type MFTDatabaseDAO used to retrieve and store information
     * @param context - activity/service context
     */
	public MFTDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
    public void onCreate(SQLiteDatabase database) {
		database.execSQL(EVENT_MESSAGE_TABLE_CREATE);
		database.execSQL(LAST_VALID_LOCATION_TABLE_CREATE);
    }

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(MFTDatabase.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		
		db.execSQL("DROP TABLE IF EXISTS " + EVENT_MESSAGE_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + LAST_VALID_LOCATION_TABLE_NAME);
		onCreate(db);
	}
}
